/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m001.bl;

import ep.persistence.controllers.T001UserJpaController;
import ep.persistence.controllers.T002DoctorJpaController;
import ep.persistence.controllers.T003PharmacistJpaController;
import ep.persistence.controllers.T004InsuranceJpaController;
import ep.persistence.entities.RegisterDoctorBean;
import ep.persistence.entities.RegisterPharmacistBean;
import ep.persistence.entities.T001User;
import ep.persistence.entities.T002Doctor;
import ep.persistence.entities.T003Pharmacist;
import ep.persistence.entities.T004Insurance;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Query;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Spyros
 */
@Path("/bl001")
public class BL001Controller {

    @POST
    @Path("/login")
    public String login(@FormParam("username") String username, @FormParam("password") String password) {
        T001User user = getUserFromUsername(username);
        if (user != null) {
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                return user.getType() + "";
            }
        }
        return null;
    }

    @GET
    @Path("/user")
    @Consumes(MediaType.TEXT_PLAIN)
//    @Produces({MediaType.APPLICATION_JSON})
    public T001User getUserFromUsername(@QueryParam("username") String username) {
        T001UserJpaController t001UserJpaController = new T001UserJpaController();
        Query query = t001UserJpaController.getEntityManager().createNamedQuery("T001User.findByUsername", T001User.class);
        query.setParameter("username", username);
        List result = query.getResultList();
        if (!result.isEmpty()) {
            return (T001User) query.getResultList().iterator().next();
        } else {
            return null;
        }
    }

    @GET
    @Path("/insurances")
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public Collection<T004Insurance> getInsurances() {
        T004InsuranceJpaController t004InsuranceJpaController = new T004InsuranceJpaController();
        return t004InsuranceJpaController.findT004InsuranceEntities();
    }

    private void saveUser(T001User user) throws Exception {
        T001UserJpaController jpaController = new T001UserJpaController();
        if (user.getT002DoctorCollection() == null) {
            ArrayList<T002Doctor> doctors = new ArrayList<T002Doctor>();
            user.setT002DoctorCollection(doctors);
        }
        if (user.getT003PharmacistCollection() == null) {
            ArrayList<T003Pharmacist> pharm = new ArrayList<T003Pharmacist>();
            user.setT003PharmacistCollection(pharm);
        }
        if (user.getId() == null) {
            jpaController.create(user);
        } else {
            jpaController.edit(user);
        }
    }

    @POST
    @Path("/rpharm")
//    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public Boolean registerPharmacist(RegisterPharmacistBean bean) {
        try {
            T001User user = bean.getUser();
            T003Pharmacist pharmacist = bean.getPharmacist();
            saveUser(user);
            pharmacist.setT001User(user);
            T003PharmacistJpaController jpaController = new T003PharmacistJpaController();
            if (pharmacist.getId() == null) {
                jpaController.create(pharmacist);
            } else {
                jpaController.edit(pharmacist);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
            return false;
        }
    }

    @POST
    @Path("/rdoctor")
//    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.TEXT_PLAIN)
    public String registerDoctor(RegisterDoctorBean bean) {
        try {
            T001User user = bean.getUser();
            T002Doctor doctor = bean.getDoctor();
            Collection insurances = bean.getInsurances();
            saveUser(user);
            doctor.setT004Insurances(insurances);
            doctor.setT001User(user);
            T002DoctorJpaController jpaController = new T002DoctorJpaController();
            if (doctor.getId() == null) {
                jpaController.create(doctor);
            } else {
                jpaController.edit(doctor);
            }
            return "0";
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
            return "-1";
        }
    }
}
